package objects
{
    import away3DExtensions.ColoredSubGeometry;
    
    import away3d.core.base.IRGBABufferRenderable;
    import away3d.core.base.SubGeometry;
    import away3d.materials.DefaultMaterialBase;
    import away3d.materials.MaterialBase;
    import away3d.materials.methods.BufferColorMethod;
    import away3d.primitives.Plane;
    
    import flash.display.BitmapData;
    
    public class ColorPlane extends Plane
    {
        var bitmapData : BitmapData;
        
        public function ColorPlane(material :DefaultMaterialBase, width:Number=100, height:Number=100, segmentsW:uint=1, segmentsH:uint=1, yUp:Boolean=true)
        {
            super(material, width, height, segmentsW, segmentsH, yUp, ColoredSubGeometry);
            
            material.addMethod(new BufferColorMethod (false, false, false));
        }
        
        /**
         * @inheritDoc
         */
        override protected function buildGeometry(target : SubGeometry) : void
        {
            var vertices : Vector.<Number>;
            var colorVertices : Vector.<Number>;
            var normals : Vector.<Number>;
            var tangents : Vector.<Number>;
            var indices : Vector.<uint>;
            var x : Number, y : Number;
            var numInds : uint;
            var base : uint;
            var tw : uint = _segmentsW+1;
            var numVerts : uint = (_segmentsH + 1) * tw;
            
            if (numVerts == target.numVertices) {
                vertices = target.vertexData;
                colorVertices = (target as IRGBABufferRenderable).getRGBAVertices();
                normals = target.vertexNormalData;
                tangents = target.vertexTangentData;
                indices = target.indexData;
            }
            else {
                vertices = new Vector.<Number>(numVerts * 3, true);
                colorVertices = new Vector.<Number>(numVerts * 4, true);
                normals = new Vector.<Number>(numVerts * 3, true);
                tangents = new Vector.<Number>(numVerts * 3, true);
                indices = new Vector.<uint>(_segmentsH * _segmentsW * 6, true);
            }
            
            numVerts = 0;
            var numColors : int = 0;
            
            for (var yi : uint = 0; yi <= _segmentsH; ++yi) {
                for (var xi : uint = 0; xi <= _segmentsW; ++xi) {
                    x = (xi/_segmentsW-.5)*_width;
                    y = (yi/_segmentsH-.5)*_height;
                    
                    vertices[numVerts] = x;
                    
                    normals[numVerts] = 0;
                    tangents[numVerts++] = 1;
                    
                    colorVertices[numColors++] = Math.random();
                    colorVertices[numColors++] = Math.random();
                    colorVertices[numColors++] = Math.random();
                    colorVertices[numColors++] = Math.random();
                    
                    if (_yUp) {
                        vertices[numVerts] = y;
                        normals[numVerts] = 0;
                        tangents[numVerts++] = 0;
                        
                        vertices[numVerts] = 1;
                        normals[numVerts] = -1;
                        tangents[numVerts++] = 0;
                    }
                    else {
                        vertices[numVerts] = -1;
                        normals[numVerts] = 1;
                        tangents[numVerts++] = 0;
                        
                        vertices[numVerts] = y;
                        normals[numVerts] = 0;
                        tangents[numVerts++] = 0;
                    }
                    
                    
                    
                    if (xi != _segmentsW && yi != _segmentsH) {
                        base = xi + yi*tw;
                        indices[numInds++] = base;
                        indices[numInds++] = base + tw;
                        indices[numInds++] = base + tw + 1;
                        indices[numInds++] = base;
                        indices[numInds++] = base + tw + 1;
                        indices[numInds++] = base + 1;
                    }
                }
            }
            
            target.updateVertexData(vertices);
            (target as IRGBABufferRenderable).updateRGBAVertices(colorVertices);
            target.updateVertexNormalData(normals);
            target.updateVertexTangentData(tangents);
            target.updateIndexData(indices);
        }
    }
}